package cn.yangyumanage.model;

import java.io.IOException;

import cn.yangyumanage.application.DataApplication;
import cn.yangyumanage.bean.LoginInfo;
import cn.yangyumanage.bean.util.LoginContainer;
import cn.yangyumanage.callback.DataCallBack;
import cn.yangyumanage.callback.HttpCallBack;
import cn.yangyumanage.config.IpConfig;
import cn.yangyumanage.http.HttpClient;
import cn.yangyumanage.util.Aes;

public class LoginHttpModel implements BaseModel {

    //登录
    public void login(LoginInfo loginInfo, DataCallBack dataCallBack) {
        String url = IpConfig.AGREEMENT + IpConfig.SERVERIP + "/login";
        String loginInfoJson = DataApplication.gson.toJson(loginInfo);
        HttpClient.asyncPost(url, loginInfoJson, new HttpCallBack() {
            @Override
            public void onSuccess(String msg) {
                try {
                    String decrypt = Aes.decrypt(msg);
                    LoginContainer container = DataApplication.gson.fromJson(decrypt, LoginContainer.class);
//                    Log.i("qweqwe", container.getToken());
                    DataApplication.token = container.getToken();
                    dataCallBack.getData(container.getCode());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(IOException e) {
                e.printStackTrace();
            }
        }, HttpClient.PUBLIC);
    }

    @Override
    public void onDestroy() {

    }
}
